# -*- coding: utf-8 -*-
from django.contrib import admin
from django import forms
from django.core.urlresolvers import reverse
from django.contrib.flatpages.admin import FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from main.models import Unit, Profession, Employee, DismissedEmployee, CurrentSite, XLS_templates
from tinymce.widgets import TinyMCE

class TinyMCEFlatPageAdmin(FlatPageAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'content':
            return forms.CharField(widget=TinyMCE(
                attrs={'cols': 80, 'rows': 30},
                mce_attrs={'external_link_list_url': reverse('tinymce.views.flatpages_link_list')},
            ))
        return super(TinyMCEFlatPageAdmin, self).formfield_for_dbfield(db_field, **kwargs)

admin.site.unregister(FlatPage)
admin.site.register(FlatPage, TinyMCEFlatPageAdmin)

class UnitAdmin(admin.ModelAdmin):
    list_display = ['title', 'abb']

admin.site.register(Unit, UnitAdmin)

class ProfessionAdmin(admin.ModelAdmin):
    list_display = ['title', 'code']
    
admin.site.register(Profession, ProfessionAdmin)

class EmployeeAdmin(admin.ModelAdmin):
    fieldsets = (
        (u'Личные данные', {
            'fields' : ('lastname', 'name', 'patronymic', 'passport',
                        'birthday', 'gender', 'address', 'phone',
                        'mobile_phone', 'kvartal', 'email', 'photo', 'dismissed')
        }),
        (u'Профессия', {
            'classes': ['wide', 'extrapretty'],
            'fields' :('tabel_number', 'permanent_number',
                       'profession', 'unit', 'actual_unit', 'worker_or_rsis', 'operative_repair',
                        'vacation_privilege')
        })
    )
    list_display = ['lastname', 'name', 'birthday', 'unit_link', 'vacation_privilege', 'operative_repair', 'dismissed']
    list_display_links = ['lastname', 'name']
    list_filter = ['actual_unit', 'vacation_privilege', 'operative_repair', 'dismissed']
    search_fields = ['lastname', 'name', 'patronymic',
                     'tabel_number', 'permanent_number', 'passport',
                     'phone', 'mobile_phone', 'address']

    def unit_link(self, obj):
        url = reverse("admin:main_unit_change", args=[obj.actual_unit.pk])
        return u'<a href="%s" title="Посмотреть данные подразделения %s" class="order_link"><strong>%s</strong></a>' %\
               (url, obj.actual_unit.title, obj.actual_unit.abb)

    unit_link.allow_tags = True
    unit_link.short_description = u'Подразделение'

    def changelist_view(self, request, extra_context=None): # http://goo.gl/fH5VO o_O
        test = request.META['HTTP_REFERER'].split(request.META['PATH_INFO'])
        if test[-1] and not test[-1].startswith('?'):
            q = request.GET.copy()
            if not request.GET.has_key('dismissed__exact'):
                q['dismissed__exact'] = '0'
            if not request.GET.has_key('operative_repair__exact'):
                q['operative_repair__exact'] = '0'
            request.GET = q
            request.META['QUERY_STRING'] = request.GET.urlencode()
        return super(EmployeeAdmin,self).changelist_view(request, extra_context=extra_context)

class XLS_templatesAdmin(admin.ModelAdmin):
    list_display = ['name']

admin.site.register(Employee, EmployeeAdmin)
admin.site.register(CurrentSite)
admin.site.register(XLS_templates,XLS_templatesAdmin)
#admin.site.register(DismissedEmployee) # remove from admin interface 